/**
 * Filename: TodayShareActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/6/29 2:57
 */
package com.github.postsanf.yinian.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.utils.Log;

/**
 * 红包分享页
 */
public class TodayShareActivityRedpacket extends BaseActivity implements View.OnClickListener {
    private Toolbar mToolbar;
    private ImageView btn_share_wx_timeline;
    private ImageView btn_share_wx_friend;
    private ImageView btn_share_qq_timeline;
    private ImageView btn_share_qq;
    private String shareStr;
    private UMShareAPI mShareAPI;
    private final String title = "亲爱的，分享一张打印优惠码给你~";
    private final String desc = "进入忆年【印品商城】就可以使用优惠码，打印照片和其他订制化小礼物喔~";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_today_share2);
        shareStr = getIntent().getExtras().getString(ACTIONs.aAddData, "");
        mShareAPI = UMShareAPI.get(this);
        initView();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.today_share_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);

        String[] mPermissionList = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS};
        ActivityCompat.requestPermissions(TodayShareActivityRedpacket.this, mPermissionList, 100);

        btn_share_wx_timeline = (ImageView) findViewById(R.id.btn_share_wx_timeline);
        btn_share_wx_friend = (ImageView) findViewById(R.id.btn_share_wx_friend);
        btn_share_qq_timeline = (ImageView) findViewById(R.id.btn_share_qq_timeline);
        btn_share_qq = (ImageView) findViewById(R.id.btn_share_qq);

        btn_share_wx_timeline.setOnClickListener(this);
        btn_share_wx_friend.setOnClickListener(this);
        btn_share_qq_timeline.setOnClickListener(this);
        btn_share_qq.setOnClickListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        String url = getIntent().getExtras().getString("url");
        UMWeb web = new UMWeb(url);
        web.setTitle(title);//标题
        web.setDescription(desc);//描述
        switch (v.getId()) {
            // 朋友圈
            case R.id.btn_share_wx_timeline:
                new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
                        .withMedia(web)
                        .share();
                Intent statintentfriend = new Intent(ACTIONs.record);
                statintentfriend.putExtra("key", "redpacketFriendCircle_6");
                statintentfriend.putExtra("value", 1);
                sendBroadcast(statintentfriend);
                break;
            // 微信好友
            case R.id.btn_share_wx_friend:
                new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
                        .withMedia(web)
                        .share();
                Intent statintentwx = new Intent(ACTIONs.record);
                statintentwx.putExtra("key", "redpacketWechat_6");
                statintentwx.putExtra("value", 1);
                sendBroadcast(statintentwx);
                break;
            // qq空间
            case R.id.btn_share_qq_timeline:
                new ShareAction(this).setPlatform(SHARE_MEDIA.QZONE).setCallback(umShareListener)
                        .withMedia(web)
                        .share();
                Intent statintentzone = new Intent(ACTIONs.record);
                statintentzone.putExtra("key", "redpacketQZone_6");
                statintentzone.putExtra("value", 1);
                sendBroadcast(statintentzone);
                break;
            // qq
            case R.id.btn_share_qq:
                new ShareAction(this).setPlatform(SHARE_MEDIA.QQ).setCallback(umShareListener)
                        .withMedia(web)
                        .share();
                Intent statintentqq = new Intent(ACTIONs.record);
                statintentqq.putExtra("key", "redpacketQQ_6");
                statintentqq.putExtra("value", 1);
                sendBroadcast(statintentqq);
                break;
        }
    }


    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (platform.name().equals("WEIXIN_FAVORITE")) {
                Toast.makeText(TodayShareActivityRedpacket.this, platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(TodayShareActivityRedpacket.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
                finish();
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(TodayShareActivityRedpacket.this, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(TodayShareActivityRedpacket.this, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    // 友盟统计
    public void onResume() {
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
        super.onResume();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
